<template>
  <div id="login">
    <section>
      <img :src="img" alt>
    </section>
    <el-form
      ref="ruleForm"
      :model="ruleForm"
      status-icon
      :rules="rules"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="手机号" prop="phone">
        <el-input v-model.number="ruleForm.phone" />
      </el-form-item>
      <el-form-item label="旧密码" prop="name">
        <el-input v-model.number="ruleForm.name" />
      </el-form-item>
      <el-form-item label="新密码" prop="pwd">
        <el-input v-model="ruleForm.pwd" type="password" autocomplete="off" />
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input v-model="ruleForm.checkPass" type="password" autocomplete="off" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">修改</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import staffApi from '../../api/staff.js'

export default {
  data() {
    var checkAge = (rule, value, callback) => {
      const obj = {}
      if (!value) {
        return callback(new Error('用户名不能为空'))
      }
      setTimeout(async() => {
        obj.pwd = value
        obj.phone = this.ruleForm.phone
        const data = await staffApi.checkOld(obj)
        console.log(data)
        if (!data) {
          callback(new Error('密码错误！'))
        } else {
          this.ruleForm._id = data[0]._id
          callback()
        }
      }, 1000)
    }
    var validatePass3 = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('手机号不能为空'))
      } else {
        callback()
      }
    }

    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass')
        }
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm.pwd) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        pwd: '',
        checkPass: '',
        name: '',
        phone: '',
        _id: ''
      },
      rules: {
        pwd: [{ validator: validatePass, trigger: 'blur' }],
        phone: [{ validator: validatePass3, trigger: 'blur' }],
        checkPass: [{ validator: validatePass2, trigger: 'blur' }],
        name: [{ validator: checkAge, trigger: 'blur' }]
      },
      img: require('../../assets/image/logo.png')
    }
  },
  methods: {
    submitForm(formName) {
      const that = this
      this.$refs[formName].validate(async valid => {
        if (valid) {
          const { pwd, _id } = that.ruleForm
          await staffApi.editPwd({ pwd, _id })
          this.$message({
            message: '修改成功',
            type: 'success'
          })
          this.$refs[formName].resetFields()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style scoped>
#login {
  width: 600px;
  margin: auto;
  margin-top: 30px;
}
img {
  width: 100px;
  height: 100px;
}
section {
  overflow: hidden;
  width: 100px;
  height: 100px;
  margin: auto;
  margin-bottom: 100px;
}
</style>
